
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0838
 * @Author : WangRuoyu 
 * @Date : 2023/9/18 16:48
 */

#include "iostream"
#include "algorithm"

using namespace std;

const int N = 1e5 + 10;
int h[N];
int cnt = 0;

void down(int x) {
    int t = x;
    if (2 * x <= cnt && h[t] > h[2 * x]) t = 2 * x;
    if (2 * x + 1 <= cnt && h[t] > h[2 * x + 1]) t = 2 * x + 1;
    if (t != x) {
        swap(h[x], h[t]);
        down(t);
    }
}

int main() {
    int k;
    scanf("%d %d", &cnt, &k);

    for (int i = 1; i <= cnt; ++i) {
        scanf("%d", &h[i]);
    }

    for (int i = cnt / 2; i; --i) {
        down(i);
    }

    for (int i = 0; i < k; ++i) {
        printf("%d ", h[1]);
        h[1] = h[cnt--];
        down(1);
    }

    return 0;
}